#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <cmath>
#include <vector>
#include <random>

// 粒子结构
struct Particle {
    sf::Vector2f position;
    sf::Vector2f velocity;
    sf::Color color;
    float lifetime;
    float maxLifetime;
};

// 生成爱心形状的点
sf::Vector2f heartShape(float t, float size = 1.0f) {
    // 参数方程生成爱心形状
    float x = 16 * std::sin(t) * std::sin(t) * std::sin(t);
    float y = -(13 * std::cos(t) - 5 * std::cos(2 * t) - 2 * std::cos(3 * t) - std::cos(4 * t));
    return sf::Vector2f(x * size, y * size);
}

int main() {
    // 创建窗口
    sf::RenderWindow window(sf::VideoMode(800, 600), "炫酷爱心动画", sf::Style::Close);
    window.setFramerateLimit(60);

    // 创建爱心形状
    sf::ConvexShape heart;
    heart.setPointCount(100);
    heart.setFillColor(sf::Color(255, 0, 0));
    heart.setOutlineThickness(2.f);
    heart.setOutlineColor(sf::Color(255, 100, 100));
    
    // 设置爱心位置
    heart.setPosition(400, 300);
    heart.setScale(2.f, 2.f);

    // 粒子系统
    std::vector<Particle> particles;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(-1.0, 1.0);
    std::uniform_real_distribution<> colorDis(150, 255);
    std::uniform_real_distribution<> lifetimeDis(1.0, 3.0);

    // 时钟用于动画
    sf::Clock clock;
    sf::Clock particleClock;
    
    // 背景渐变矩形
    sf::RectangleShape background(sf::Vector2f(800, 600));
    sf::VertexArray gradient(sf::Quads, 4);
    gradient[0].position = sf::Vector2f(0, 0);
    gradient[1].position = sf::Vector2f(800, 0);
    gradient[2].position = sf::Vector2f(800, 600);
    gradient[3].position = sf::Vector2f(0, 600);
    
    // 初始背景颜色
    gradient[0].color = sf::Color(10, 10, 40);
    gradient[1].color = sf::Color(30, 10, 50);
    gradient[2].color = sf::Color(10, 20, 40);
    gradient[3].color = sf::Color(20, 10, 30);

    // 主循环
    while (window.isOpen()) {
        // 事件处理
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }

        // 更新爱心形状和颜色
        float time = clock.getElapsedTime().asSeconds();
        float pulse = std::sin(time * 2.0f) * 0.1f + 1.0f;
        heart.setScale(2.f * pulse, 2.f * pulse);
        
        // 更新爱心颜色
        int red = 200 + 55 * std::sin(time * 0.7f);
        int green = 50 + 50 * std::sin(time * 1.3f);
        int blue = 50 + 50 * std::sin(time * 0.9f);
        heart.setFillColor(sf::Color(red, green, blue));
        
        // 更新爱心轮廓
        heart.setOutlineColor(sf::Color(red + 50, green + 50, blue + 50));

        // 更新爱心点
        for (unsigned int i = 0; i < heart.getPointCount(); ++i) {
            float t = 2 * static_cast<float>(i) / heart.getPointCount() * 3.14159f;
            heart.setPoint(i, heartShape(t, 1.0f));
        }

        // 生成新粒子
        if (particleClock.getElapsedTime().asSeconds() > 0.01f) {
            particleClock.restart();
            
            for (int i = 0; i < 5; ++i) {
                Particle p;
                float t = static_cast<float>(std::rand()) / RAND_MAX * 2 * 3.14159f;
                sf::Vector2f point = heartShape(t, 15.0f);
                p.position = heart.getTransform().transformPoint(point);
                p.velocity = sf::Vector2f(dis(gen) * 50.f, dis(gen) * 50.f);
                p.color = sf::Color(colorDis(gen), colorDis(gen) / 3, colorDis(gen) / 2, 200);
                p.lifetime = p.maxLifetime = lifetimeDis(gen);
                particles.push_back(p);
            }
        }

        // 更新粒子
        for (auto& p : particles) {
            p.position += p.velocity * 0.016f; // 假设60FPS
            p.velocity *= 0.99f; // 减速
            p.lifetime -= 0.016f;
            p.color.a = static_cast<sf::Uint8>(200 * (p.lifetime / p.maxLifetime));
        }

        // 移除死亡粒子
        particles.erase(std::remove_if(particles.begin(), particles.end(),
            [](const Particle& p) { return p.lifetime <= 0.f; }), particles.end());

        // 更新背景渐变
        float colorChange = std::sin(time * 0.3f) * 0.5f + 0.5f;
        gradient[0].color = sf::Color(10, 10, 40 + 10 * colorChange);
        gradient[1].color = sf::Color(30, 10, 50 + 10 * colorChange);
        gradient[2].color = sf::Color(10, 20, 40 + 10 * colorChange);
        gradient[3].color = sf::Color(20, 10, 30 + 10 * colorChange);

        // 绘制
        window.clear();
        
        // 绘制背景
        window.draw(gradient);
        
        // 绘制粒子
        for (const auto& p : particles) {
            sf::CircleShape particleShape(2.f);
            particleShape.setFillColor(p.color);
            particleShape.setPosition(p.position);
            window.draw(particleShape);
        }
        
        // 绘制爱心
        window.draw(heart);
        
        // 显示文字
        sf::Font font;
        if (font.loadFromFile("arial.ttf")) { // 如果你有字体文件
            sf::Text text("Love is in the air!", font, 30);
            text.setPosition(250, 50);
            text.setFillColor(sf::Color(255, 255, 255, 128 + 127 * std::sin(time * 2.0f)));
            window.draw(text);
        }

        window.display();
    }

    return 0;
}